package order

import (
	"database/sql"
	"parking-lot-go-admin/model"
	"time"
)

type OrderXiaoqiangPaymentStruct struct {
	Id                       int       `gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	OrderNo                  string    `gorm:"type:varchar(255); COMMENT:'系统单号'"`
	XqOrderNo                int       `gorm:"type:int(11); COMMENT:'小强单号'"`
	ReservationEndTimeBefore time.Time `gorm:"type:datetime; COMMENT:'原预约时间'"`
	ReservationEndTimeAfter  time.Time `gorm:"type:datetime; COMMENT:'修改后预约时间'"`
	CreatedAt                time.Time `gorm:"type:datetime"`
	UpdatedAt                time.Time `gorm:"type:datetime"`
	Money                    int       `gorm:"type:money"`
}

func (c *OrderXiaoqiangPaymentStruct) TableName() string {
	return "order_xiaoqiang_payment"
}

// 订单补缴金额
func (c *OrderXiaoqiangPaymentStruct) PaymentMoeny(orderNo, xqOrderNo string) float64 {
	var money sql.NullFloat64

	db := model.Db

	db.Table(c.TableName()).
		Select("COALESCE(SUM(money), 0) as money").
		Where("order_no", orderNo).
		Where("xq_order_no", xqOrderNo).
		Scan(&money)

	if money.Valid {
		// money 包含有效的 float64 值
		actualMoney := money.Float64 / 100
		return actualMoney
	} else {
		// money 是 NULL
		return 0.0 // 或者根据需要返回其他默认值
	}
}

// 订单补缴金额
func (c *OrderXiaoqiangPaymentStruct) PaymentMoenyNew(where map[string]interface{}) float64 {
	var money sql.NullFloat64

	db := model.Db

	db = db.Table(c.TableName()).Debug()
	db = db.Select("COALESCE(SUM(money), 0) as money")

	//where查询
	for field, value := range where {
		db = db.Where(field, value)
	}

	db = db.Scan(&money)
	if money.Valid {
		// money 包含有效的 float64 值
		actualMoney := money.Float64 / 100
		return actualMoney
	} else {
		// money 是 NULL
		return 0.0 // 或者根据需要返回其他默认值
	}
}
